"""父类"""
import time

import allure
from selenium import webdriver


class BasePage:
    _BASE_URL = ""

    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(10)
            self.driver.maximize_window()

        if not self.driver.current_url.startswith("http"):
            self.driver.get(self._BASE_URL)

    def do_find(self, by, locator=None):
        """获取单个元素"""
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, locator=None):
        """获取多个元素"""
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def do_send_keys(self, value, by, locator=None):
        ele = self.do_find(by, locator)
        ele.clear()
        ele.send_keys(value)

    def do_quit(self):
        self.driver.quit()

    def get_screenshot(self):
        # 时间戳
        timestamp = int(time.time())
        # 提前创建好image文件夹
        image_path = f"./images/image_{timestamp}.png"
        # 截图保存到image文件夹
        self.driver.save_screenshot(image_path)
        # 将截图添加到报告中
        allure.attach.file(image_path, name="picture",
                           attachment_type=allure.attachment_type.PNG)


